package com.example.administrator.hqq.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.module.AddModuleActivity;
import com.example.administrator.hqq.adapter.FragmentsAdapter;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.bean.ConventionListBean;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.fragment.discover.ConventionFragment;
import com.example.administrator.hqq.fragment.discover.MVPConventionFragment;
import com.example.administrator.hqq.fragment.discover.UnConventionalityFragment;
import com.zhy.autolayout.AutoRelativeLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 发现fragment
 */
public class DiscoverFragment extends MyFragemnt {

    @BindView(R.id.normal)
    AutoRelativeLayout normal;
    @BindView(R.id.normal_auto)
    AutoRelativeLayout normalAuto;
    @BindView(R.id.discover_vp)
    ViewPager discoverVp;
    Unbinder unbinder;
    private View inflate;
    private List<Fragment> fragments;
    private String TAG = this.getClass().getSimpleName();
    private ConventionFragment conventionFragment;
    private MineFragment.AddModule addModule;

    public DiscoverFragment() {
        // Required empty public constructor
    }

    public void setAddModule(MineFragment.AddModule addModule) {
        this.addModule = addModule;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_discover, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initview();
        return inflate;
    }

    private void initview() {
        fragments = new ArrayList<>();
        conventionFragment = new ConventionFragment();
        //mvp版本的圈子页面
        MVPConventionFragment mvpConventionFragment = new MVPConventionFragment();
        fragments.add(mvpConventionFragment);
        UnConventionalityFragment unConventionalityFragment = new UnConventionalityFragment();
//        fragments.add(unConventionalityFragment);
        conventionFragment.setAddModule(new MineFragment.AddModule() {
            @Override
            public void addModule(String s) {
                Intent intent1 = new Intent(getContext(), AddModuleActivity.class);
                intent1.putExtra(LoongsConfig.type, AddModuleActivity.Type.MODIFICATION);
                intent1.putExtra(LoongsConfig.data, (Serializable) getData());
                intent1.putExtra(LoongsConfig.category_id, s);
                startActivity(intent1);
            }
        });
        FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(getChildFragmentManager(), fragments);
        discoverVp.setAdapter(fragmentsAdapter);
//        normalAuto.setSelected(true);
        discoverVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
//                    normalAuto.setSelected(true);
//                    abnormalAuto.setSelected(false);
                } else {
//                    abnormalAuto.setSelected(true);
//                    normalAuto.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public List<ConventionListBean.DataBean> getData() {
        return conventionFragment.getData();
    }

    //  R.id.abnormal_auto
    @OnClick({R.id.normal_auto})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.normal_auto:
                discoverVp.setCurrentItem(0);
//                normalAuto.setSelected(true);
//                abnormalAuto.setSelected(false);
                break;
//            case R.id.abnormal_auto:
//                discoverVp.setCurrentItem(1);
//                abnormalAuto.setSelected(true);
//                normalAuto.setSelected(false);
//                break;
        }
    }
}
